package com.pty.mercuryservice.controller;

import com.pty.mercuryservice.model.User;
import com.pty.mercuryservice.service.UserService;
import com.pty.mercuryservice.util.SignatureUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by Empty on 2017/7/4.
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/byName/{username}", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Object> getUserByName(@PathVariable String username) {
        return new ResponseEntity<>(userService.getUserByName(username), HttpStatus.OK);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String getUserByName(@RequestBody Map<String, String> param) {
        User user = User.builder().username(param.get("username")).password(SignatureUtils.getMD5Code(param.get("password"))).userCode(param.get("usercode")).build();
        userService.save(user);
        return "user " + user.getUsername() + " saved";
    }


}
